    //
//  QuizViewController.m
//  PPL
//
//  Created by Tin Nguyen Trung on 3/26/14.
//  Copyright (c) 2014 Familly. All rights reserved.
//

#import "QuizViewController.h"
#import "ONViewController.h"

@interface QuizViewController () 
{
    int questCount;
}

@end

@implementation QuizViewController

- (void)didClickRadioButtonAtIndex:(NSInteger)index andTitle:(NSString *)title {
    [self.lbQuestNumb setText:title];
    [self.selectedQuestionImage setImage:[UIImage imageNamed:@"on"]];
}

-(void)doChooseButton: (UIButton *)sender {
    
    NSString *title = sender.titleLabel.text;
    [self didClickRadioButtonAtIndex:sender.tag  andTitle:title];
    for (UIButton *btn in self.arrButton) {
        [btn setBackgroundImage:[UIImage imageNamed:@"off"] forState:UIControlStateNormal];
    }
    [sender setBackgroundImage:[UIImage imageNamed:@"on"] forState:UIControlStateNormal];
}


- (void)getArrayTitleButton:(NSArray *)arrTitleButton {
    self.arrButton = [[NSMutableArray alloc] init];
    int x = 0;
    int contentWith = 0;
    for (int i = 0 ; i < [arrTitleButton count]; i++) {
        NSString *titleButton = [arrTitleButton objectAtIndex:i];
        CGSize sizeTitle =  [titleButton sizeWithFont:[UIFont systemFontOfSize:20.f]];
        sizeTitle.width += 25;
        if (i == 0) {
            x = 10;
        }
        else {
            x +=60;
            
        }
        contentWith +=60;
        
        UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(x , 0, 60, 60);
        button.tag = i;
        
        [button addTarget:self action:@selector(doChooseButton:) forControlEvents:UIControlEventTouchUpInside];
        [button setBackgroundImage:[UIImage imageNamed:@"off.png"] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button setTitle:titleButton forState:UIControlStateNormal];
        [self.arrButton addObject:button];
        [self.questNumberIcon addSubview:button];
        [self.questNumberIcon setContentSize:CGSizeMake(contentWith+10, button.frame.size.height-10)];
    }
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    self.lbTimer.text = @"00.00.00";
    running = FALSE;
    startDate = [NSDate date];
    
    if(!running){
        running = TRUE;
        if (stopTimer == nil) {
            stopTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/10.0
                                                         target:self
                                                       selector:@selector(updateTime)
                                                       userInfo:nil
                                                        repeats:YES];
        }
    }else{
        running = FALSE;
        [stopTimer invalidate];
        stopTimer = nil;
    }
    
    self.lbMode.text = [[NSString stringWithFormat:@"%@ MODE",self.modestring] uppercaseString];
    self.lbQuest.text = [[NSString stringWithFormat:@"%@ QUESTIONS" ,self.questionsString] lowercaseString];
    
    NSMutableArray *arrBtn = [[NSMutableArray alloc] init];
    
    for (int i=0; i < 30;i++) {
        NSString *item = [NSString stringWithFormat:@"%d", i+1];
        [arrBtn addObject:item];
    }
    
    [self getArrayTitleButton:arrBtn];
    self.lbTimer.text = @" ";
}


- (void)updateTime {
    NSDate *currentDate = [NSDate date];
    NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:startDate];
    NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"mm:ss"];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]];
    NSString *timeString=[dateFormatter stringFromDate:timerDate];
    
    
    self.lbTimer.text = timeString;}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
